package com.android.miniskirt.ui;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.android.miniskirt.R;
import com.android.miniskirt.miniApplication;
import com.android.miniskirt.service.MQTTService;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class bubble9 extends Activity {
    /** Called when the activity is first created. */
    ListView itemlist = null;
    Button mButton;
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(2);
    private miniApplication mApplication;
    SimpleAdapter notes;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bubble);
        itemlist = (ListView) findViewById(R.id.list1);
        mButton = (Button)findViewById(R.id.Button_Send);
        mButton.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				   // http地址  
				  String httpUrl = "http://mobileloc.ddns.mobi?method=send&from=15110291483&to=18611182251&msg=liucheng";
				  //HttpGet连接对象  
				  HttpGet httpRequest = new HttpGet(httpUrl);  
				  //取得HttpClient对象  
				  HttpClient httpclient = new DefaultHttpClient();  
				  //请求HttpClient，取得HttpResponse  
				  HttpResponse httpResponse = null;
				try {
					httpResponse = httpclient.execute(httpRequest);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
				  //请求成功  
				  if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)  
				 {  
				  //取得返回的字符串  
					  String strResult = null;
				  try {
					strResult = EntityUtils.toString(httpResponse.getEntity());
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}  
				Toast toast = Toast.makeText(getApplicationContext(), strResult, Toast.LENGTH_LONG);
				toast.show();
				 }  
				 else 
				 {  
					 Toast toast = Toast.makeText(getApplicationContext(), "请求错误", Toast.LENGTH_LONG);
						toast.show();
				 }  
			} 
		});
        refreshListItems();
        //启动更新消息工作者线程
        new receiverMsg().execute((Void)null);
    }
    private void refreshListItems() {
        buildListForSimpleAdapter();
        notes = new SimpleAdapter(this, list, R.layout.item,
                        new String[] { "str" }, new int[] { R.id.TextView01,});
        itemlist.setAdapter(notes);
//        itemlist.setSelection(0);
}
    private void buildListForSimpleAdapter() {
//        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(2);
        // Build a map for the attributes
        Intent intent = getIntent();
        String notificationMessage = intent
                .getStringExtra(MQTTService.MQTT_MSG_NOTIFY_MSG);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("str", notificationMessage);
        list.add(map);
}
    public class receiverMsg extends AsyncTask<Void,Void,Void> {

		/* (non-Javadoc)
		 * @see android.os.AsyncTask#onProgressUpdate(Progress[])
		 */
		@SuppressWarnings("unchecked")
		@Override
		protected void onProgressUpdate(Void... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
			WeakReference<Map<String, Object>>  map = new WeakReference<Map<String, Object>> (null);
	        ((Map<String, Object>) map).put("str", mApplication.mMessage);
	        list.add((Map<String, Object>) map);
	        notes.notifyDataSetChanged();
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			while(true){
				if(mApplication.mIsNewMessage){
					publishProgress();
				}
			}//while end of 
		}
    	
    }
}